﻿using JetBrains.Annotations;
using NineSeas.Models;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using System;

namespace NineSeas.Drivers
{
    [UsedImplicitly]
    public class NIneSeasSettingsDriver : ContentPartDriver<NineSeasSettingsPart>
    {
        protected override string Prefix
        {
            get { return "NineSeasSettings"; }
        }

        // GET
        protected override DriverResult Editor(NineSeasSettingsPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_NineSeasSettings_SiteSettings",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/NineSeasSettings.SiteSettings",
                    Model: part,
                    Prefix: Prefix))
                    .OnGroup("NineSeasSettings");
        }

        // POST
        protected override DriverResult Editor(NineSeasSettingsPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }
        protected override void Exporting(NineSeasSettingsPart part, ExportContentContext context)
        {
            var element = context.Element(part.PartDefinition.Name);
            element.SetAttributeValue("FbCommentsNumberOfPosts", part.FbCommentsNumberOfPosts);
            element.SetAttributeValue("FbCommentsWidth", part.FbCommentsWidth);
            element.SetAttributeValue("FbCommentsColorScheme", part.FbCommentsColorScheme);
            element.SetAttributeValue("FbLikesWidth", part.FbLikesWidth);
            element.SetAttributeValue("FbLikesColorScheme", part.FbLikesColorScheme);
            element.SetAttributeValue("FbLikesEnableSendButton", part.FbLikesEnableSendButton);
            element.SetAttributeValue("FbLikesLayoutStyle", part.FbLikesLayoutStyle);
            element.SetAttributeValue("FbLikesShowFaces", part.FbLikesShowFaces);
            element.SetAttributeValue("FbLikesVerbToDisplay", part.FbLikesVerbToDisplay);
            element.SetAttributeValue("FbLikesFont", part.FbLikesFont);
        }

        protected override void Importing(NineSeasSettingsPart part, ImportContentContext context)
        {
            var partName = part.PartDefinition.Name;
            context.ImportAttribute(partName, "FbCommentsNumberOfPosts", value => part.FbCommentsNumberOfPosts = Int32.Parse(value));
            context.ImportAttribute(partName, "FbCommentsWidth", value => part.FbCommentsWidth = Int32.Parse(value));
            context.ImportAttribute(partName, "FbCommentsColorScheme", value => part.FbCommentsColorScheme = value);
            context.ImportAttribute(partName, "FbLikesWidth", value => part.FbLikesWidth = Int32.Parse(value));
            context.ImportAttribute(partName, "FbLikesColorScheme", value => part.FbLikesColorScheme = value);
            context.ImportAttribute(partName, "FbLikesEnableSendButton", value => part.FbLikesEnableSendButton = bool.Parse(value));
            context.ImportAttribute(partName, "FbLikesLayoutStyle", value => part.FbLikesLayoutStyle = value);
            context.ImportAttribute(partName, "FbLikesShowFaces", value => part.FbLikesShowFaces = bool.Parse(value));
            context.ImportAttribute(partName, "FbLikesVerbToDisplay", value => part.FbLikesVerbToDisplay = value);
            context.ImportAttribute(partName, "FbLikesFont", value => part.FbLikesFont = value);
        }
    }
}